package com.megvii.corelib.update.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.megvii.corelib.R;
import com.megvii.corelib.base.MegviiActivity;
import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.corelib.update.param.UpdateActivityParam;
import com.megvii.corelib.update.presenter.UpdatePresenter;
import com.megvii.corelib.util.AppUtil;

/**
 * Created by linyue on 16/3/6.
 */
public class UpdateActivity extends MegviiActivity<UpdatePresenter, UpdateActivityParam>
        implements View.OnClickListener
{
    protected NumberProgressBar progressBar;

    protected Button pauseButton;

    protected Button resetButton;

    protected Button returnButton;

    private UpdateListener listener;

    private boolean forceUpdate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(activityParam.layoutId);
        progressBar = (NumberProgressBar) findViewById(R.id.update_layout_progress_bar);
        pauseButton = (Button) findViewById(R.id.update_layout_pause);
        pauseButton.setOnClickListener(this);
        resetButton = (Button) findViewById(R.id.update_layout_reset);
        resetButton.setOnClickListener(this);
        returnButton = (Button) findViewById(R.id.update_layout_return);
        returnButton.setOnClickListener(this);
    }

    public void setForceUpdate(boolean forceUpdate)
    {
        this.forceUpdate = forceUpdate;
    }

    public void setReturnButtonVisible(boolean visible)
    {
        returnButton.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setDownloading(boolean downloading)
    {
        pauseButton.setText(downloading ? "暂停" : "开始");
    }

    public void setDownloadProgress(int progress)
    {
        progressBar.setProgress(progress);
    }

    public void setDownloadProgressMax(int progressMax)
    {
        progressBar.setMax(progressMax);
    }

    @Override
    public void onClick(View v)
    {
        if (listener != null)
        {
            int id = v.getId();
            if (id == R.id.update_layout_pause)
            {
                listener.onClickPause();
            }
            else if (id == R.id.update_layout_reset)
            {
                listener.onClickReset();
            }
            else if (id == R.id.update_layout_return)
            {
                listener.onClickReturn();
            }
        }
    }

    @Override
    public void finish()
    {
        super.finish();
        overridePendingTransition(0, R.anim.fade_out);
    }

    @Override
    public Class getPresenterClass()
    {
        return UpdatePresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    public void setListener(UpdateListener listener)
    {
        this.listener = listener;
    }

    @Override
    public void onBackPressed()
    {
        if (forceUpdate)
        {
            AppUtil.FakeExitApp(this);
        }
        else
        {
            super.onBackPressed();
        }
    }

    public interface UpdateListener
    {
        public void onClickPause();

        public void onClickReset();

        public void onClickReturn();
    }
}
